<template>
	<view class="header-box">
		<view class="header-box-main">
			<text>{{title}}</text>
			<view class="back-btn" @click="toBack" v-if="isBack"><image class="back-btn-icon" src="../../static/img/back-icon.png" mode=""></image></view>
			<view class="share-btn" @click="shareClick" v-if="isShare"><image class="share-btn-icon" src="../../static/img/share-icon.png" mode=""></image></view>
		</view>
		<share-page v-if="sharePage" :shareUrl="shareUrl" :copyUrl="copyUrl" @close="closeShare"></share-page>
	</view>
</template>

<script>
	// #ifdef H5
	// const jweixin = require('jweixin-module') 
	// #endif
	export default {
		name:"header-box",
		data() {
			return {
				sharePage:false,
				shareUrl:'',
				userInfo:null,
				copyUrl:'',
			};
		},
		props:{
			title:{
				type:String,
				default:''
			},
			isShare:{
				type:Boolean,
				default:true
			},
			isBack:{
				type:Boolean,
				default:false
			}
		},
		created() {
			this.$on('onShareClick',e => {
				console.log(e)
			})
		},
		methods:{
			toBack() {
				uni.navigateBack({
					delta:1
				})
			},
			shareClick() {
				// #ifdef H5
				this.userInfo = uni.getStorageSync('userInfo') || '';
				if (window) {
					if (this.userInfo) {
						let link = window.location.href+'&userCode='+this.userInfo.userCode;
						this.copyUrl = link;
						this.$api.qrCodeCreate({content:link}).then(res => {
							// console.log(res)
							if (res.code == 200) {
								this.shareUrl = res.data.url;
							} else {
								this.shareUrl = link;
							}
							this.sharePage = true;
						})
					} else {
						let link = window.location.href;
						this.copyUrl = link;
						this.$api.qrCodeCreate({content:link}).then(res => {
							// console.log(res)
							if (res.code == 200) {
								this.shareUrl = res.data.url;
							} else {
								this.shareUrl = link;
							}
							this.sharePage = true;
						})
					}
				}
				// if (this.isWeiXinLogin()) {
				// 	jweixin.ready(function(){
					    
				// 	});
				// } else {
				// 	uni.showToast({
				// 		title:'请在微信内打开分享',
				// 		icon:'none'
				// 	})
				// }
				// #endif
			},
			closeShare(e) {
				console.log(e)
				this.sharePage = e;
			},
		}
	}
</script>

<style lang="less">
	.header-box{
		width: 100%;
		height: 88rpx;
		// background-color: #16181D;
		font:caption;
		position: sticky;
		top: 0;
		z-index: 999;
		.header-box-main{
			height: 88rpx;
			font-size: 36rpx;
			color: #ffffff;
			text-align: center;
			line-height: 88rpx;
			position: relative;
			.back-btn{
				width: 62rpx;
				height: 62rpx;
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;
				margin-left: 44rpx;
				.back-btn-icon{
					width: 100%;
					height: 100%;
				}
			}
			.share-btn{
				width: 48rpx;
				height: 48rpx;
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;
				margin-right: 48rpx;
				.share-btn-icon{
					width: 100%;
					height: 100%;
				}
			}
		}
	}
</style>
